package com.ecoolex.chinadutyfree.retailer.admin.graphql;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.chinadutyfree.retailer.admin.core.AdminConstant;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoods;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsMarketing;
import com.ecoolex.chinadutyfree.retailer.entity.DirectMailGoodsMarketing.Column;
import com.ecoolex.chinadutyfree.retailer.pojo.AddDirectMailGoodsMarketingDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.EditDirectMailGoodsMarketingDTO;
import com.ecoolex.chinadutyfree.retailer.pojo.QueryDirectMailGoodsMarketingDTO;
import com.ecoolex.chinadutyfree.retailer.ws.DirectMailGoodsService;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.google.common.collect.Lists;


@GraphQL2Module(type = Type.Admin, description = "直邮商品营销管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY,
	key = DirectMailGoodsMarketingGraphql.AUTH,
	name = "直邮商品营销管理")
public class DirectMailGoodsMarketingGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-DIRECT-MAIL-GOODS-MARKETING";

	@Reference
	private DirectMailGoodsService service;

	@TypeField(type = DirectMailGoodsMarketing.class, field = "directMailGoods", description = "直邮商品")
	public DirectMailGoods directMailGoods(@Source DirectMailGoodsMarketing source) {
		return service.getById(source.getDirectMailGoodsId());
	}
	
	@TypeField(type = DirectMailGoods.class, field = "existMarketingConfig", description = "是否存在于营销配置")
	public boolean existMarketingConfig(@Source DirectMailGoods source, @NonNull String position, String positionValue) {
		return service.existMarketing(position, positionValue, source.getDirectMailGoodsId());
	}

	private static final String AUTH_GET = AUTH + "-GET";

	@QueryField(field = "directMailGoodsMarketing", description = { "获取直销商品营销详情" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "获取直销商品营销详情")
	public DirectMailGoodsMarketing get(@NonNull Integer id, AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.getMarketingById(id);
	}

	private static final String AUTH_QUERY = AUTH + "-QUERY";

	@QueryField(field = "directMailGoodsMarketingList", description = { "查询直销商品营销列表" })
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "查询直销商品营销列表")
	public Pagination<DirectMailGoodsMarketing> query(@NonNull QueryDirectMailGoodsMarketingDTO query, @NonNull Sorting[] sorts, @NonNull Page page, AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return service.queryMarketing(query, sorts, page);
	}

	private static final String AUTH_ADD = AUTH + "-ADD";

	@MutationField(field = "addDirectMailGoodsMarketing", description = "添加直销商品营销")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加直销商品营销")
	public DirectMailGoodsMarketing add(@NonNull AddDirectMailGoodsMarketingDTO add, AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		return service.addMarketing(add, user.userName());
	}

	private static final String AUTH_EDIT = AUTH + "-EDIT";

	@MutationField(field = "editDirectMailGoodsMarketing", description = "编辑直销商品营销")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑直销商品营销")
	public DirectMailGoodsMarketing edit(@NonNull EditDirectMailGoodsMarketingDTO edit, AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		DirectMailGoodsMarketing record = service.getMarketingById(edit.getId());
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "直销商品营销不存在");
		BeanUtil.copy(edit,
			record,
			Lists.newArrayList(Column.position,
				Column.positionValue,
				Column.directMailGoodsId,
				Column.deleted,
				Column.createUser,
				Column.createTime).stream().map(Column::getJavaProperty).toArray(String[]::new));
		return service.updateMarketing(record, user.userName());
	}

	private static final String AUTH_DEL = AUTH + "-DEL";

	@MutationField(field = "delDirectMailGoodsMarketing", description = "删除直销商品营销")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除直销商品营销")
	public boolean del(@NonNull Integer id, AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		DirectMailGoodsMarketing record = service.getMarketingById(id);
		Assert.isTrue(Check.notNull(record), ResultCode.VERIFY.build(1), "直销商品营销不存在");
		service.deleteMarketing(record, user.userName());
		return true;
	}
}

